package com.sk.bilibili.utils;

import android.graphics.Color;

import com.sk.bilibili.bean.Danmu;

import java.util.Arrays;
import java.util.HashSet;
import java.util.Random;
import java.util.Set;

import master.flame.danmaku.danmaku.model.BaseDanmaku;
import master.flame.danmaku.danmaku.model.android.DanmakuContext;

public class DanmuUtils {

    public static BaseDanmaku toDanmaku(Danmu danmu, DanmakuContext context, boolean user) {
        BaseDanmaku danmaku = context.mDanmakuFactory.createDanmaku(checkType(danmu.getType()),context);
        danmaku.time=danmu.getTime().longValue();
        danmaku.textColor=danmu.getColor()|ALPHA;
        danmaku.textSize=60;
        danmaku.text=danmu.getContent();
        if(user)danmaku.borderColor= Color.BLUE;
        return danmaku;
    }

    // 排除非法类型
    private static int checkType(Integer type) {
        if(LEGAL_TYPE.contains(type))return type;
        return getRandomType();
    }

    private static final int ALPHA=0xFF000000;
    private static final int[] COLORS={0xFE0302,0xFE0302,0xFFAA02,0xFFD302,0xFFD302,
            0xA0EE00,0xA0EE00,0x019899,0x4266BE,0x89D5FF,0xCC0273,0x222222,0x9B9B9B,0x9B9B9B};
    private static final Integer[] TYPES={BaseDanmaku.TYPE_SCROLL_RL,BaseDanmaku.TYPE_SCROLL_LR,
            BaseDanmaku.TYPE_FIX_TOP,BaseDanmaku.TYPE_FIX_BOTTOM};
    private static final Set<Integer> LEGAL_TYPE=new HashSet<Integer>(Arrays.asList(TYPES));

    private static Random random=new Random();

    public static int getRandomColor(){
        return COLORS[random.nextInt(COLORS.length)];
    }

    public static int getRandomType(){
        return TYPES[random.nextInt(TYPES.length)];
    }

}